package lab1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * this a utility class that help to achieve advanced function of this project
 * it can distribute the file location by the type of file
 * and be able to loop through a s specified html file to find out all of the http address in it;
 * @author Weihan li
 * @version 1.5
 *
 */
public class Utilities {
	private Request user;
	private Response web;
	/**
	 * need the into decide file location;
	 * @param web response class for current connection
	 * @param user request class for user setting
	 */
	public Utilities(Response web, Request user) {
		this.user=user;
		this.web=web;
	}
	/**
	 * generate file location based on user's choice and info from the connection;
	 * get file type from connection header
	 * @return the aim file;
	 */
	public File getFile(){
		String address=user.getFileLocation();
		String type=web.getContentType();
		String domain=user.getHostName();
		String fileclass=type.substring(0, type.indexOf('/'));
		Random ran= new Random();
		int random=ran.nextInt(9999999);
		String fileName= type.replace("\\",'\\'+domain+'_'+fileclass+'_'+random+'.');
		if(address.charAt(address.length()-1)=='\\'){
			address+=fileName;
		}else{
			address+='\\'+fileName;
		}
		return new File(address);
	}
	//	public String toFile(String content){
	//
	//		//String address="d:\\donwnLoad";
	//
	//		//String content=web.getEntityBody();
	//		//FileWriter writer=new FileWriter("c:\\Result.txt", true);
	//		Writer writer = null;
	//		try{
	//			File file = new File(address);
	//			writer = new BufferedWriter(new FileWriter(file));
	//			writer.write(content);
	//		} catch (FileNotFoundException e){
	//			e.printStackTrace();
	//		} catch (IOException e){
	//			e.printStackTrace();
	//		}finally{
	//			if (writer != null){
	//				try {
	//					writer.close();
	//				} catch (IOException e) {
	//					e.printStackTrace();
	//				}
	//			}
	//		}
	//		return fileName;
	//	
	/**
	 * this class will go through the file parameter and find out all of the url address, and use them to generate a list of Request,
	 * sort the list based the host name of the url and return it; 
	 * @param file the file that will  loop through;
	 * @return return the sorted list of Request
	 */

	public  Request[] loopURL(File file){
		String content="";
		FileInputStream reader=null;
		try {
			reader = new FileInputStream(file);
			int data;   
			while ((data = reader.read()) != -1) {  
				content += (char) data;  
			}   
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		content.replace("\"/", user.getHostName()+'/');
		ArrayList<Request> url=new ArrayList<Request>();
		Pattern pattern = Pattern.compile("^(http|www|ftp|)?(://)?(//w+(-//w+)*)(//.(//w+(-//w+)*))*((://d+)?)(/(//w+(-//w+)*))*(//.?(//w)*)(//?)?(((//w*%)*(//w*//?)*(//w*:)*(//w*//+)*(//w*//.)*(//w*&)*(//w*-)*(//w*=)*(//w*%)*(//w*//?)*(//w*:)*(//w*//+)*(//w*//.)*(//w*&)*(//w*-)*(//w*=)*)*(//w*)*)$",Pattern.CASE_INSENSITIVE );   
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()){
			url.add(new Request(matcher.group()));
		}

		return sortURL(url);
	}
	/**
	 * this is a helper class that sort the Request list based on it's host name
	 * @param aim the unsorted Request list
	 * @return the sorted Request list
	 */
	private Request[] sortURL (ArrayList<Request> aim){
		Comparator<Request> urlCompare = new URLComparator();
		Collections.sort(aim, urlCompare);
		Request[] retVal= new Request[aim.size()];
		return aim.toArray(retVal);

	}
}
